<?php
namespace app\extensions\lib;
class Message
{
    public static  $type = 0;
    const ANALYTICS_LABEL = 'tapplus_push';
    public static $fcm_common = [
        'name' => null,
        'data' => null,
        "notification" => null,
        "android" => null,
        "webpush" => null,
        "apns" => null,
        "fcm_options" => ['analytics_label'=>self::ANALYTICS_LABEL],
        "token" => null,
        "topic" => null,
        "condition" => null,
    ];
    public static $hms_common = [
        'message' => ['android'=>[],'fcm_options'=>['analytics_label'=>self::ANALYTICS_LABEL]],
        'validate_only' => false,
    ];
    public static $ios_common = [
        'aps' => ['alert'=>null],
    ];
    public static $version = '';

    public static function setVersion($version){
        self::$version = $version;
    }


    /**
     * 设置通知 基本字段
     * @param $notification
     * @return bool
     */
    public static function setNotification($notification){
        if(!is_array($notification)) $notification = json_decode($notification,true);

        self::$fcm_common['notification'] = $notification;
        self::$ios_common['aps']['alert'] = $notification;

        self::$hms_common['message']['notification'] = $notification;
        return true;
    }

    public static function setSound($sound){
        self::$fcm_common['android']['notification']['sound'] = $sound;
        self::$ios_common['aps']['sound'] = $sound;
        self::$hms_common['message']['android']['notification']['sound'] = $sound;
        return true;
    }

    public static function setImage($imageUrl){
        self::$fcm_common['notification']['image'] = $imageUrl;
        //self::$ios_common['aps']['alert']['image'] = self::$ios_common['aps']['extras']['image'] = $imageUrl;
        self::$ios_common['media'] = ['type'=>'image','url' =>$imageUrl];
        //"media":{"type":"image","url":"https://www.fotor.com/images2/features/photo_effects/e_bw.jpg"}
        self::$hms_common['message']['android']['notification']['image'] = $imageUrl;
        return true;
    }

    public static function setColor($value){
        self::$fcm_common['android']['notification']['color'] = $value;
        self::$hms_common['message']['android']['notification']['color'] = $value;
    }

    public static function setBadge($value){
        self::$fcm_common['android']['notification']['notification_count'] = $value;
        self::$ios_common['aps']['badge'] = $value;
        self::$hms_common['message']['android']['notification']['notification_count'] = $value;
        return true;
    }

    public static function setToken($token){
        self::$fcm_common['token'] = $token;
        self::$hms_common['message']['token'] = $token;
    }

    public static function setSubtitle($subtitle){
        self::$ios_common['aps']['alert']['subtitle'] = $subtitle;

    }

    public static function setData($data){
        self::$fcm_common['data']['pext'] = json_encode($data);
        self::$fcm_common['data']['title'] = self::$fcm_common['notification']['title'];
        self::$fcm_common['data']['body'] = self::$fcm_common['notification']['body'];
        //self::$ios_common['aps']['alert']['pext'] = json_encode($data);
        self::$ios_common['aps']['pext'] = json_encode($data);
        self::$hms_common['message']['data']['pext'] = json_encode($data);;
    }



    public static function setAndroid($data){
        self::$fcm_common['android'] = $data;
    }
    public static function setAndroidNotification($data){
        if(!isset(self::$fcm_common['android'])){
            self::$fcm_common['android'] = [];
        }
        if(!isset(self::$fcm_common['android']['notification'])){
            self::$fcm_common['android']['notification'] = self::$fcm_common['notification'];
        }
        self::$fcm_common['android']['notification'] += $data;
    }
    public static function setIosNotification($data){
        self::$ios_common['aps'] += $data;
    }
    public static function setIos($data){
        self::$ios_common['aps']['alert'] += $data;
    }
    public static function setCategory($value){
        self::$ios_common['aps']['category'] = $value;
    }

    public static function get(){
        $return =  [
            1 => ['message' => array_filter(self::$fcm_common)],
            2 => array_filter(self::$ios_common),
            3 => array_filter(self::$hms_common),
        ];
        if(self::$type==1){
            unset($return[1]['message']['notification']);
            unset($return[1]['message']['android']['notification']);
            unset($return[1]['message']['data']['title']);
            unset($return[1]['message']['data']['body']);

            unset($return[3]['message']['notification']);
            unset($return[3]['message']['android']['notification']);
            unset($return[3]['message']['data']['title']);
            unset($return[3]['message']['data']['body']);

            unset($return[2]['aps']['alert']);
            unset($return[2]['media']);
        }
        self::$type = 0;//还原默认
        return $return;
    }

    public static function init(array $old)
    {
        self::$fcm_common = $old[1]['message'];
        self::$ios_common = $old[2];

        self::$hms_common = $old[3];
        self::$version  = '';
    }

    public static function setType($type)
    {
        self::$type = $type;

    }

    public static function setFcmOption($fcm_option)
    {
        self::$fcm_common['fcm_options'] = array_merge(self::$fcm_common['fcm_options'],$fcm_option);
        self::$hms_common['message']['fcm_options'] = array_merge(self::$hms_common['message']['fcm_options'],$fcm_option);
    }


}